/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * @file    stm32g4xx_it.c
 * @brief   Interrupt Service Routines.
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2024 STMicroelectronics.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32g4xx_it.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN TD */

/* USER CODE END TD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP */

#include "adc.h"
/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

#include "MY_INC/APP/APP_ACLoad_Sys.h"
#include "MY_INC/PHY/PHY_Print.h"

/* USER CODE END 0 */

/* External variables --------------------------------------------------------*/

/* USER CODE BEGIN EV */
uint8_t Tx1_flag;
uint8_t Tx2_flag;
extern UART_DMA_BufferTypeDef Uart1_DMA_Buffer;
extern UART_DMA_BufferTypeDef Uart2_DMA_Buffer;
extern volatile uint8_t uart1_is_sending; // 添加外部变量声明
extern volatile uint8_t uart2_is_sending; // 添加外部变量声明

// DMA2 Channel3中断状态标志
// 0: 无中断
// 1: 传输完成
// 2: 半传输完成
// 3: 传输错误
volatile uint8_t dma2_ch3_status = 0;
volatile uint8_t dma2_ch1_status = 0;
/* USER CODE END EV */

/******************************************************************************/
/*           Cortex-M4 Processor Interruption and Exception Handlers          */
/******************************************************************************/
/**
  * @brief This function handles Non maskable interrupt.
  */
void NMI_Handler(void)
{
  /* USER CODE BEGIN NonMaskableInt_IRQn 0 */

  /* USER CODE END NonMaskableInt_IRQn 0 */
  /* USER CODE BEGIN NonMaskableInt_IRQn 1 */
  while (1)
  {
  }
  /* USER CODE END NonMaskableInt_IRQn 1 */
}

/**
  * @brief This function handles Hard fault interrupt.
  */
void HardFault_Handler(void)
{
  /* USER CODE BEGIN HardFault_IRQn 0 */

  /* USER CODE END HardFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_HardFault_IRQn 0 */

    /* USER CODE END W1_HardFault_IRQn 0 */
  }
}

/**
  * @brief This function handles Memory management fault.
  */
void MemManage_Handler(void)
{
  /* USER CODE BEGIN MemoryManagement_IRQn 0 */

  /* USER CODE END MemoryManagement_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */
    /* USER CODE END W1_MemoryManagement_IRQn 0 */
  }
}

/**
  * @brief This function handles Prefetch fault, memory access fault.
  */
void BusFault_Handler(void)
{
  /* USER CODE BEGIN BusFault_IRQn 0 */

  /* USER CODE END BusFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_BusFault_IRQn 0 */
    /* USER CODE END W1_BusFault_IRQn 0 */
  }
}

/**
  * @brief This function handles Undefined instruction or illegal state.
  */
void UsageFault_Handler(void)
{
  /* USER CODE BEGIN UsageFault_IRQn 0 */

  /* USER CODE END UsageFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_UsageFault_IRQn 0 */
    /* USER CODE END W1_UsageFault_IRQn 0 */
  }
}

/**
  * @brief This function handles System service call via SWI instruction.
  */
void SVC_Handler(void)
{
  /* USER CODE BEGIN SVCall_IRQn 0 */

  /* USER CODE END SVCall_IRQn 0 */
  /* USER CODE BEGIN SVCall_IRQn 1 */

  /* USER CODE END SVCall_IRQn 1 */
}

/**
  * @brief This function handles Debug monitor.
  */
void DebugMon_Handler(void)
{
  /* USER CODE BEGIN DebugMonitor_IRQn 0 */

  /* USER CODE END DebugMonitor_IRQn 0 */
  /* USER CODE BEGIN DebugMonitor_IRQn 1 */

  /* USER CODE END DebugMonitor_IRQn 1 */
}

/**
  * @brief This function handles Pendable request for system service.
  */
void PendSV_Handler(void)
{
  /* USER CODE BEGIN PendSV_IRQn 0 */

  /* USER CODE END PendSV_IRQn 0 */
  /* USER CODE BEGIN PendSV_IRQn 1 */

  /* USER CODE END PendSV_IRQn 1 */
}

/**
  * @brief This function handles System tick timer.
  */
void SysTick_Handler(void)
{
  /* USER CODE BEGIN SysTick_IRQn 0 */

  /* USER CODE END SysTick_IRQn 0 */
  HAL_IncTick();
  /* USER CODE BEGIN SysTick_IRQn 1 */

  /* USER CODE END SysTick_IRQn 1 */
}

/******************************************************************************/
/* STM32G4xx Peripheral Interrupt Handlers                                    */
/* Add here the Interrupt Handlers for the used peripherals.                  */
/* For the available peripheral interrupt handler names,                      */
/* please refer to the startup file (startup_stm32g4xx.s).                    */
/******************************************************************************/

/**
  * @brief This function handles USART1 global interrupt / USART1 wake-up interrupt through EXTI line 25.
  */
void USART1_IRQHandler(void)
{
  /* USER CODE BEGIN USART1_IRQn 0 */
  if (LL_USART_IsActiveFlag_IDLE(USART1))
  {
    // 清除空闲中断
    LL_USART_ClearFlag_IDLE(USART1);
    // 更新写指针位置
    Uart1_DMA_Buffer.write_index = MAX_SIZE - LL_DMA_GetDataLength(DMA2, LL_DMA_CHANNEL_1);
  }
  /* USER CODE END USART1_IRQn 0 */
  /* USER CODE BEGIN USART1_IRQn 1 */

  /* USER CODE END USART1_IRQn 1 */
}

/**
  * @brief This function handles USART2 global interrupt / USART2 wake-up interrupt through EXTI line 26.
  */
void USART2_IRQHandler(void)
{
  /* USER CODE BEGIN USART2_IRQn 0 */
  if (LL_USART_IsActiveFlag_IDLE(USART2)) //
  {
    // 清除空闲中断
    LL_USART_ClearFlag_IDLE(USART2);
    // 更新写指针位置
    Uart2_DMA_Buffer.write_index = MAX_SIZE - LL_DMA_GetDataLength(DMA2, LL_DMA_CHANNEL_3);
  }
  /* USER CODE END USART2_IRQn 0 */
  /* USER CODE BEGIN USART2_IRQn 1 */

  /* USER CODE END USART2_IRQn 1 */
}

/**
  * @brief This function handles DMA2 channel1 global interrupt.
  */
void DMA2_Channel1_IRQHandler(void)
{
  /* USER CODE BEGIN DMA2_Channel1_IRQn 0 */

  // 如果不清除标志位，会一直触发中断导致死循环
  if (LL_DMA_IsActiveFlag_TC1(DMA2))
  {
    LL_DMA_ClearFlag_TC1(DMA2); // 清除传输完成标志
    dma2_ch1_status = 1;        // 传输完成
  }
  else if (LL_DMA_IsActiveFlag_HT1(DMA2))
  {
    LL_DMA_ClearFlag_HT1(DMA2); // 清除半传输完成标志
    dma2_ch1_status = 2;        // 半传输完成
  }
  else if (LL_DMA_IsActiveFlag_TE1(DMA2))
  {
    LL_DMA_ClearFlag_TE1(DMA2); // 清除传输错误标志
    dma2_ch1_status = 3;        // 传输错误
    // 发生错误时禁用DMA通道
    LL_DMA_DisableChannel(DMA2, LL_DMA_CHANNEL_1);
  }
  /* USER CODE END DMA2_Channel1_IRQn 0 */
  /* USER CODE BEGIN DMA2_Channel1_IRQn 1 */

  /* USER CODE END DMA2_Channel1_IRQn 1 */
}

/**
  * @brief This function handles DMA2 channel2 global interrupt.
  */
void DMA2_Channel2_IRQHandler(void)
{
  /* USER CODE BEGIN DMA2_Channel2_IRQn 0 */
  // 串口1发送中断
  if (LL_DMA_IsActiveFlag_TC2(DMA2))
  {
    LL_DMA_ClearFlag_TC2(DMA2);
    LL_DMA_DisableChannel(DMA2, LL_DMA_CHANNEL_2);
    uart1_is_sending = 0;        // 清除发送标志
  }
  /* USER CODE END DMA2_Channel2_IRQn 0 */
  /* USER CODE BEGIN DMA2_Channel2_IRQn 1 */

  /* USER CODE END DMA2_Channel2_IRQn 1 */
}

/**
  * @brief This function handles DMA2 channel3 global interrupt.
  */
void DMA2_Channel3_IRQHandler(void)
{
  /* USER CODE BEGIN DMA2_Channel3_IRQn 0 */
  // 检查传输完成中断
  if (LL_DMA_IsActiveFlag_TC3(DMA2))
  {
    // 清除传输完成标志
    LL_DMA_ClearFlag_TC3(DMA2);
    dma2_ch3_status = 1; // 传输完成
  }
  // 检查半传输完成中断
  else if (LL_DMA_IsActiveFlag_HT3(DMA2))
  {
    // 清除半传输完成标志
    LL_DMA_ClearFlag_HT3(DMA2);
    dma2_ch3_status = 2; // 半传输完成
  }
  // 检查传输错误中断
  else if (LL_DMA_IsActiveFlag_TE3(DMA2))
  {
    // 清除传输错误标志
    LL_DMA_ClearFlag_TE3(DMA2);
    dma2_ch3_status = 3; // 传输错误
    // 发生错误时禁用DMA通道
    LL_DMA_DisableChannel(DMA2, LL_DMA_CHANNEL_3);
  }
  /* USER CODE END DMA2_Channel3_IRQn 0 */
  /* USER CODE BEGIN DMA2_Channel3_IRQn 1 */

  /* USER CODE END DMA2_Channel3_IRQn 1 */
}

/**
  * @brief This function handles DMA2 channel4 global interrupt.
  */
void DMA2_Channel4_IRQHandler(void)
{
  /* USER CODE BEGIN DMA2_Channel4_IRQn 0 */
  // 串口2发送中断
  if (LL_DMA_IsActiveFlag_TC4(DMA2))
  {
    LL_DMA_ClearFlag_TC4(DMA2);
    LL_DMA_DisableChannel(DMA2, LL_DMA_CHANNEL_4);
    uart2_is_sending = 0; // 清除发送标志
  }
  /* USER CODE END DMA2_Channel4_IRQn 0 */
  /* USER CODE BEGIN DMA2_Channel4_IRQn 1 */

  /* USER CODE END DMA2_Channel4_IRQn 1 */
}

/**
  * @brief This function handles HRTIM master timer global interrupt.
  */
void HRTIM1_Master_IRQHandler(void)
{
  /* USER CODE BEGIN HRTIM1_Master_IRQn 0 */
  //    if (LL_HRTIM_IsActiveFlag_REP(HRTIM1, LL_HRTIM_TIMER_MASTER))
  //    {
  //        LL_HRTIM_ClearFlag_REP(HRTIM1, LL_HRTIM_TIMER_MASTER);
  //        // 业务代码
  //        HRTIM_MASTER_REP_IRQ();
  //    }
  if (LL_HRTIM_IsActiveFlag_CMP1(HRTIM1, LL_HRTIM_TIMER_MASTER))
  {
    LL_HRTIM_ClearFlag_CMP1(HRTIM1, LL_HRTIM_TIMER_MASTER);
    // 业务代码
    HRTIM_MASTER_COMP1_IRQ();
  }
  /* USER CODE END HRTIM1_Master_IRQn 0 */
  /* USER CODE BEGIN HRTIM1_Master_IRQn 1 */

  /* USER CODE END HRTIM1_Master_IRQn 1 */
}

/* USER CODE BEGIN 1 */
////
/* USER CODE END 1 */
